<?php
/***
 * 素材API Service
 *
 *
 * @author    lanhongjie <lanhj@yunys.com.cn>
 * @copyright 2016 QuanZhouYunYiShang Inc.
 */

namespace App\Services\SourcePicService;


use App\Services\ImgServices\ImgTool;
use App\Services\ModifyResourcePriceRecordsService\ModifyResourcePriceRecordsService;
use App\Services\ProductTempService\ProductTempTool;
use App\Services\TagService\TagTool;
use App\Services\Tool\GeneralTool;
use App\Services\Tool\Jpush\JpushTool;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class SourcePicApiService
{
     protected $sourcePicTool;
     protected $imgTool;
    /**
     * SourcePicApiService constructor.
     */
    public function __construct(SourcePicTool $sourcePicTool,ImgTool $imgTool)
    {
        $this->sourcePicTool=$sourcePicTool;
        $this->imgTool=$imgTool;
    }

    /***
     * 上传素材
     * @param $data
     * @return bool
     */
    public function uploadSourcePic($data)
    {
        try{
            $image_url=$data['image_url'];
            unset($data['image_url']);
            $img_path=ImgTool::getImgPathByImgUrl($image_url);
            if(empty($img_path)){
                return false;
            }
            $image_thumbnail_url=$this->imgTool->getImgThumbByImgPath($img_path,600,600,true);
            $image_small_url=$this->imgTool->getImgThumbByImgPath($img_path,300,300,true);
            $data['thumbnail']=$image_thumbnail_url;
            $data['small_image']=$image_small_url;
            $data['original_image']=$img_path;
            $data['state']=1;
            $data['created_at']=time();
            $data['updated_at']=time();
            $source_pic_id=GeneralTool::only_no();
            $data['source_pic_id']=$source_pic_id;
            $num = DB::table('source_pics')->insert($data);
            if($num==1){
                $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($data['source_pic_id'],1);
                if(empty($source_pic)){
                    return false;
                }
                if($source_pic->original_price==0){
                    $price='免费';
                }else{
                    $price='¥'.GeneralTool::priceFormat($source_pic->original_price);
                }
                $user_id=$data['user_id'];
                $push_message='【素材审核中】 你有⼀- 个素材审核中>>查看详情';
                $message='出售价格：'.$price.'\n';
                if($source_pic->end_time==0){
                    $message.='下架时间：暂无';
                }else{
                    $message.='下架时间：'.date('Y年m月d日 H:i',$source_pic->end_time);
                }
                JpushTool::push(
                    $push_message,
                    0,
                    $user_id.'',
                    ['title'=>'素材审核中',
                        'user_id'=>$user_id.'',
                        'class'=>'system_notification',
                        'type'=>1,
                        'jump_value'=>'',
                        'template_no'=>'4',
                        'message'=>$message,
                        'images'=>[['original_image'=>$source_pic->original_image,'thumbnail'=>$source_pic->small_image]]
                    ]);
            }
            return ($num==1)?$source_pic_id:false;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /***
     * 更新素材
     * @param $data
     * @return bool
     */
    public function updateSourcePic($data)
    {
        try{
            $source_pic_id=$data['source_pic_id'];
            $user_id=$data['user_id'];
            $data['state']=1;
            $data['updated_at']=time();
            $num = DB::table('source_pics')->where(array('source_pic_id'=>$source_pic_id,'user_id'=>$user_id))
                ->update($data);
//            if($num==1){
//                $source_pic=$this->sourcePicTool->getSourcePicBySourcePicId($data['source_pic_id'],1);
//                if(!empty($source_pic)){
//                    false;
//                }
//                $user_id=$data['user_id'];
//                if($source_pic->price==0){
//                    $price='免费';
//                }else{
//                    $price='¥'.GeneralTool::priceFormat($source_pic->price);
//                }
//                $push_message='【素材审核中】你有⼀个素材素材审核中>>查看详情';
//                $message='出售价格：'.$price.'\n';
//                if($source_pic->end_time==0){
//                    $message.='下架时间：暂无';
//                }else{
//                    $message.='下架时间：'.date('Y年m月d日 H:i',$source_pic->end_time);
//                }
//                JpushTool::push(
//                    $push_message,
//                    0,
//                    $user_id.'',
//                    ['title'=>'素材审核中',
//                        'user_id'=>$user_id.'',
//                        'class'=>'system_notification',
//                        'type'=>1,
//                        'jump_value'=>'',
//                        'template_no'=>'4',
//                        'message'=>$message,
//                        'images'=>[['original_image'=>$source_pic->original_image,'thumbnail'=>$source_pic->small_image]]
//                    ]);
//            }
            return ($num==1)?true:false;
        }catch (\Exception $e){
            Log::info($e);
            return false;
        }
    }

    /**
     * 获取tag列表
     * @param $input
     * @return array
     */
    public function getTagList(array $input){
        $per_page = $input['per_page'];//每页页数
        $product_temp_id = $input['product_temp_id'];


        $tag_ids=array();
        //标签对当前模板授权可以使用
        $auz_tag_ids=DB::table('resource_auzs')->where(array(
            'state'=>1,
            'user_resource_id'=>$product_temp_id,
            'user_resource_id_type'=>1,
            'auz_id_type'=>3,
            'auz_type'=>1,
        ))->pluck('auz_id')
        ->toArray();

        $auz_tag_id_ints=array();
        foreach ($auz_tag_ids as $auz_tag_id){
            $auz_tag_id_ints[]=$auz_tag_id;
        }
        //获取素材共享Tag
        $share_tag_ids=DB::table('tags')->where(array(
            'state'=>1,
            'app_id'=>1,
            'has_sub_tag'=>0,
            'is_show'=>1
            ))->pluck('id')
            ->toArray();
        $available_tag_ids=array_merge($auz_tag_id_ints,$share_tag_ids);
        $available_tag_ids=array_unique($available_tag_ids);

        //当前模板是不是只用某些素材
        if(!empty($product_temp_id)){
            $is_use_specified_source_pic=ProductTempTool::isUseSpecifiedSourcePic($product_temp_id);
        }

        if(isset($is_use_specified_source_pic)&&($is_use_specified_source_pic === 1)){
            $share_tag_ids=DB::table('resource_auzs')->where(array(
                'state'=>1,
                'auz_id'=>$product_temp_id,
                'auz_type'=>2,
                'auz_id_type'=>1,
                'user_resource_id_type'=>3,
            ))->pluck('user_resource_id')
            ->toArray();
            //可用素材标签数组和能用素材标签数组交集
            $tag_ids=array_intersect($available_tag_ids,$share_tag_ids);
        }else{
            $tag_ids=$available_tag_ids;
        }
        $tag_ids=array_unique($tag_ids);
        $tags=DB::table('tags')
            ->where(array('state'=>1,'app_id'=>1))
            ->whereIn('id',$tag_ids)
            ->orderBy('tag_type', 'ASC')
            ->orderBy('sort', 'ASC')
            ->orderBy('updated_at', 'desc')
            ->select("id as tag_id","name as tag_name",'tag_type','original_image')
            ->paginate($per_page)
            ->toArray();
        foreach ($tags['data'] as $tag){
            if(!empty($tag->original_image)){
                $original_image=ImgTool::getImgUrlByImgPath($tag->original_image);
                $tag->original_image=empty($original_image)?'':$original_image;
            }
        }
        if(empty($tags['data'])){
            return array('data' => array(), 'current_page' =>1, 'per_page' =>0, 'total_page' =>0, 'total' => 0);
        }else{
            return array('data' => $tags['data'], 'current_page' => $tags['current_page'], 'per_page' => $tags['per_page'], 'total_page' => $tags['last_page'], 'total' => $tags['total']);
        }
    }

    /***
     * 按照标签Id获取素材
     * @param array $data
     * @return array
     */
    public function getSourcePicByTag(array  $data){
        $tag_id=$data['tag_id'];
        $per_page = $data['per_page'];
        $product_temp_id = $data['product_temp_id'];
        $tag=$this->sourcePicTool->getTagByTagId($tag_id);
        if($tag===null){
            return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0);
        }
        if($tag->tag_type==1&&($tag->sort_type==1)){
         //按照最热排序
            $source_pics=self::getSourcePicBySourcePicIds(array(),2,10,'used_num');
        }elseif ($tag->tag_type==1&&($tag->sort_type==2)){
            $source_pics=self::getSourcePicBySourcePicIds(array(),2,10,'updated_at');
        }else{
            $tag_ids=array($tag_id);
            $resource_ids = DB::table('tag_resource_maps')->where(array('state' => 1))
                ->whereIn('tag_id',$tag_ids)
                ->orderBy('updated_at', 'desc')
                ->pluck("resource_id")
                ->toArray();

            $resource_ids=array_unique($resource_ids);
            $source_pics=self::getSourcePicBySourcePicIds($resource_ids,1,10,'used_num');
        }
        //付费说明H5
        $paid_instructions_h5_url='http://gold.xitu.io/';
        if(empty($source_pics)){
            return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0,'paid_instructions_h5_url' => $paid_instructions_h5_url);
        }
        return array('data'=>$source_pics['data'],'current_page'=>$source_pics['current_page'],'per_page'=>$source_pics['per_page'],'total_page'=>$source_pics['last_page'],'total'=>$source_pics['total'],'paid_instructions_h5_url' => $paid_instructions_h5_url);
    }

    /***
     * 获取标签下的素材
     * @param array $data
     * @return array
     */
    public function getSourcePicByTagId(array $data)
    {
        try{
            $per_page = $data['per_page'];
            $product_temp_id = $data['product_temp_id'];//产品模板id
            $tag_id=$data['tag_id'];
            $tag_data['tag_id']=$tag_id;
            $tag_data['app_id']=1;
            $tag=TagTool::getTagByTagId($tag_data);
            if(empty($tag)){
                return array('data' => array(), 'current_page' => 1,
                    'per_page' => $per_page, 'total_page' => 0,
                    'total' =>0);
            }
            //不限时素材
            $infinite_time_source_pic_ids=DB::table('source_pics')
                ->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1,'is_show'=>1))
                ->where('is_infinite_time',1)
                ->pluck('source_pic_id')
                ->toArray();
            //有下架时间的素材
            $un_infinite_time_source_pic_ids=DB::table('source_pics')
                ->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1,'is_show'=>1))
                ->where('is_infinite_time',0)
                ->where('end_time','>',time())
                ->pluck('source_pic_id')
                ->toArray();
            $all_source_pic_ids=array_merge($infinite_time_source_pic_ids,$un_infinite_time_source_pic_ids);
            //标签获取全部素材
           if($tag->tag_type===1){
                //不限时素材id
               $query=DB::table('source_pics')
                   ->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1))
                   ->whereIn('source_pic_id',$all_source_pic_ids);
               if($tag->sort_type===1){
                   $query->orderBy('used_num','DESC');
               }else{
                   $query->orderBy('updated_at','DESC');
               }
               $query->select('is_infinite_stock','name as source_pic_name','is_infinite_time','source_pic_id','is_shelves','price as original_price','is_shelves','original_image','thumbnail','small_image','price as original_price','user_id','stock','audit_state');
               $source_pics=$query->paginate($per_page)->toArray();
           }else{
               $resource_ids=DB::table('tag_resource_maps')
                   ->where(array('state'=>1,'tag_id'=>$tag_id,'app_id'=>1,'is_show'=>1))
                   ->pluck('resource_id')
                   ->toArray();
               $source_pic_ids=DB::table('source_pics')
                   ->where(array(
                       'state'=>1,
                       'audit_state'=>2,
                       'is_shelves'=>1))
                   ->whereIn('source_pic_id',$resource_ids)
                   ->whereIn('source_pic_id',$all_source_pic_ids)
                   ->pluck('source_pic_id')
                   ->toArray();
               $source_pics=DB::table('tag_resource_maps')->where(array('state'=>1,'tag_id'=>$tag_id,'app_id'=>1,'is_show'=>1))
                   ->whereIn('resource_id',$source_pic_ids)
                   ->distinct()
                   ->orderBy('sort','ASC')
                   ->orderBy('updated_at','DESC')
                   ->select('resource_id as source_pic_id')
                   ->paginate($per_page)
                   ->toArray();
               foreach ($source_pics['data'] as $key=> $source_pic){
                   $source_pic_obj=DB::table('source_pics')
                       ->where('source_pic_id',$source_pic->source_pic_id)
                       ->select('is_infinite_stock','name as source_pic_name','is_infinite_time','source_pic_id','is_shelves','price as original_price','is_shelves','original_image','thumbnail','small_image','price as original_price','user_id','stock','audit_state')
                       ->first();
                   if(empty($source_pic_obj)){
                       unset($source_pics['data'][$key]);
                   }
                   $source_pic->is_infinite_stock=$source_pic_obj->is_infinite_stock;
                   $source_pic->is_infinite_time=$source_pic_obj->is_infinite_time;
                   $source_pic->source_pic_id=$source_pic_obj->source_pic_id;
                   $source_pic->is_shelves=$source_pic_obj->is_shelves;
                   $source_pic->original_price=$source_pic_obj->original_price;
                   $source_pic->is_shelves=$source_pic_obj->is_shelves;
                   $source_pic->original_image=$source_pic_obj->original_image;
                   $source_pic->thumbnail=$source_pic_obj->thumbnail;
                   $source_pic->small_image=$source_pic_obj->small_image;
                   $source_pic->original_price=$source_pic_obj->original_price;
                   $source_pic->user_id=$source_pic_obj->user_id;
                   $source_pic->stock=$source_pic_obj->stock;
                   $source_pic->audit_state=$source_pic_obj->audit_state;
               }
           }
            foreach ($source_pics['data'] as $source_pic){
                $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
                $original_price = $source_pic->original_price;
                if($original_price==0){
                    $original_price='免费';
                }else{
                    $original_price=GeneralTool::priceFormat($source_pic->original_price).'';
                }
                $source_pic->original_price=$original_price;
                $source_pic->preferential_price=$original_price;
                $source_pic->has_preferential =0;
                if(!empty($source_pic->original_image)){
                    $original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
                    $source_pic->original_image=empty($original_image)?'':$original_image;
                }
                if(!empty($source_pic->thumbnail)){
                    $thumbnail=ImgTool::getImgCDNUrlByImgPath($source_pic->thumbnail);
                    $source_pic->thumbnail=empty($thumbnail)?'':$thumbnail;
                }
                if(!empty($source_pic->small_image)){
                    $small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                    $source_pic->small_image=empty($small_image)?'':$small_image;
                }
            }
            //付费说明H5，暂时未用到
            $paid_instructions_h5_url='http://gold.xitu.io/';
            if(empty($source_pics)){
                return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0,'paid_instructions_h5_url' => $paid_instructions_h5_url);
            }
            return array('data'=>$source_pics['data'],'current_page'=>$source_pics['current_page'],'per_page'=>count($source_pics['data']),'total_page'=>$source_pics['last_page'],'total'=>$source_pics['total'],'paid_instructions_h5_url' => $paid_instructions_h5_url);
        }catch (\Exception $e){
            Log::info($e);
            return array('data' => array(), 'current_page' => 1,
                'per_page' => $per_page, 'total_page' => 0,
                'total' =>0);
        }
    }
    /***
     * 获取素材Id数组 对应详情
     * @param array $source_pic_id
     * @param int $type
     * @param int $per_page
     * @param string $orderBy
     * @return mixed
     */
    public function getSourcePicBySourcePicIds(array  $source_pic_id,$type=1,$per_page=10,$orderBy='updated_at'){
        $infinite_time_source_pic_ids=DB::table('source_pics')
            ->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1))
            ->where('is_infinite_time',1)
            ->pluck('source_pic_id')
            ->toArray();
        $un_infinite_time_source_pic_ids=DB::table('source_pics')
            ->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1))
            ->where('is_infinite_time',0)
            ->where('end_time','>',time())
            ->pluck('source_pic_id')
            ->toArray();
        $all_source_pic_ids=array_merge($infinite_time_source_pic_ids,$un_infinite_time_source_pic_ids);
        if($type==1){
            $source_pics=DB::table('source_pics')->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1))
                ->whereIn('source_pic_id',$source_pic_id)
                ->whereIn('source_pic_id',$all_source_pic_ids)
                ->orderBy($orderBy, 'desc')
                ->select('is_infinite_stock','name as source_pic_name','is_infinite_time','source_pic_id','is_shelves','price as original_price','is_shelves','original_image','thumbnail','small_image','price as original_price','user_id','stock','audit_state')
                ->paginate($per_page)
                ->toArray();

        }else{
            $source_pics=DB::table('source_pics')->where(array('state'=>1,'audit_state'=>2,'is_shelves'=>1))
                ->whereIn('source_pic_id',$all_source_pic_ids)
                ->orderBy($orderBy, 'desc')
                ->select('is_infinite_stock','name as source_pic_name','is_infinite_time','source_pic_id','is_shelves','price as original_price','original_image','thumbnail','small_image','price as original_price','user_id','stock','audit_state')
                ->paginate($per_page)
                ->toArray();
        }

        foreach ($source_pics['data'] as $source_pic){
            $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
            $original_price = $source_pic->original_price;
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($source_pic->source_pic_id,0,1);
            if($is_resource_modify_price!==false){
                $original_price=$is_resource_modify_price['modify_price'];
            }
            if($original_price==0){
                $original_price='免费';
            }else{
                $original_price=GeneralTool::priceFormat($source_pic->original_price).'';
            }
            $source_pic->original_price=$original_price;
            $source_pic->preferential_price=$original_price;
            $source_pic->has_preferential =0;
            if(!empty($source_pic->original_image)){
                $original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
                $source_pic->original_image=empty($original_image)?'':$original_image;
            }
            if(!empty($source_pic->thumbnail)){
                $thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
                $source_pic->thumbnail=empty($thumbnail)?'':$thumbnail;
            }
            if(!empty($source_pic->small_image)){
                $small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                $source_pic->small_image=empty($small_image)?'':$small_image;
            }
        }
        return $source_pics;
    }
    /****
     * 素材重新上架
     * @param array $data
     * @return bool
     */
    public function shelvesSourcePic(array $data)
    {
        if(!isset($data['user_id'])||!isset($data['source_pic_id'])){
            return false;
        }
        $source_pic_id=$data['source_pic_id'];
        $user_id=$data['user_id'];
        $data['updated_at']=time();
        $data['is_shelves']=1;
        $res = DB::table('source_pics')->where(array('state' => 1,'source_pic_id' => $source_pic_id,'user_id' => $user_id))
            ->update($data);
        return ($res==1)?true:false;
    }

    /***
     * 素材删除
     * @param array $data
     * @return bool
     */
    public function delSourcePic(array $data)
    {
        if(!isset($data['user_id'])||!isset($data['source_pic_id'])){
            return false;
        }
        $source_pic_id=$data['source_pic_id'];
        $user_id=$data['user_id'];
        $data['updated_at']=time();
        $data['state']=0;
        $res = DB::table('source_pics')->where(array('state' => 1,'source_pic_id' => $source_pic_id,'user_id' => $user_id))
            ->update($data);
        return ($res==1)?true:false;
    }
    /***
     * 下架素材
     * @param $source_pic_id
     * @return bool
     */
    public function offShelvesSourcePic($source_pic_id)
    {
        if(empty($source_pic_id)){
            return false;
        }
        $res = DB::table('source_pics')->where(array('state' => 1,'source_pic_id' => $source_pic_id))
            ->update(array('updated_at'=>time(),'is_shelves'=>0));
        return ($res>1)?true:false;
    }

    /***
     * 获取素材上架状态
     * @param $source_pic_id
     * @return int
     */
    public function getSourcePicIsShelve($source_pic_id){
        $is_shelves= DB::table('source_pics')
            ->where(array('source_pic_id' => $source_pic_id))
            ->value('is_shelves');
        return empty($is_shelves)?0:1;
    }

    /***
     * 获取素材状态
     * @param $source_pic_id
     * @return int
     */
    public function getSourcePicState($source_pic_id){
        $state= DB::table('source_pics')
            ->where(array('source_pic_id' => $source_pic_id))
            ->value('state');
        return empty($state)?0:1;
    }
    /***
     * 获取我的素材审核列表
     * @param array $data
     * @return array|bool
     */
    public function getMySourcePicAuditList(array $data)
    {
        if(!isset($data['per_page'])||!isset($data['user_id'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>0,'total_page'=>0,'total'=>0);
        }
        $per_page=$data['per_page'];
        $user_id=$data['user_id'];
        //只获取未通过和审核中的首次啊
        $source_pics = DB::table('source_pics')->where(array('state' => 1,'user_id' => $user_id))
            ->whereIn('audit_state',array(1,3))
            ->orderBy('updated_at','DESC')
            ->select("source_pic_id","original_image",'end_time','is_infinite_time','name as source_pic_name','thumbnail','is_shelves','price as original_price','small_image','user_id','audit_state')
            ->paginate($per_page);
        foreach ($source_pics as $source_pic){
            $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($source_pic->source_pic_id,0,1);
            $original_price=$source_pic->original_price;
            if($is_resource_modify_price!==false){
                $original_price=$is_resource_modify_price['modify_price'];
            }
            if($original_price==0){
                $original_price='免费';
            }else{
                $original_price=GeneralTool::priceFormat($original_price).'';
            }
            $source_pic->has_preferential =0;
            $source_pic->original_price=$original_price;
            $source_pic->preferential_price=$original_price;
            if(!empty($source_pic->original_image)){
                $original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
                $source_pic->original_image=empty($original_image)?'':$original_image;
            }
            if(!empty($source_pic->thumbnail)){
                $thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
                $source_pic->thumbnail=empty($thumbnail)?'':$thumbnail;
            }
            if(!empty($source_pic->small_image)){
                $small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                $source_pic->small_image=empty($small_image)?'':$small_image;
            }
            $source_pic->audit=SourcePicTool::getAuditByAuditState($source_pic->audit_state);
        }
        $source_pic_arr=$source_pics->toArray();
        if(empty($source_pic_arr['data'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0);
        }
        return array('data'=>$source_pic_arr['data'],'current_page'=>$source_pic_arr['current_page'],'per_page'=>$source_pic_arr['per_page'],'total_page'=>$source_pic_arr['last_page'],'total'=>$source_pic_arr['total']);
    }

    /****
     * 获取我的素材已上架列表
     * @param array $data
     * @return array
     */
    public function getMySourcePicOnTheShelvesList(array $data)
    {
        if(!isset($data['per_page'])||!isset($data['user_id'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>0,'total_page'=>0,'total'=>0);
        }
        $per_page=$data['per_page'];
        $user_id=$data['user_id'];
        //只获取未通过和审核中的首次啊
        $source_pics = DB::table('source_pics')->where(array('state' => 1,'audit_state' => 2,'is_shelves' => 1,'user_id' => $user_id))
            ->where('audit_state',2)
            ->select("source_pic_id","used_num as sell_num",'is_infinite_time','end_time','name as source_pic_name','is_shelves','audit_state',"original_image",'price as original_price','thumbnail','small_image','user_id')
            ->paginate($per_page);
        foreach ($source_pics as $source_pic){
            $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($source_pic->source_pic_id,0,1);
            $original_price=$source_pic->original_price;
            if($is_resource_modify_price!==false){
                $original_price=$is_resource_modify_price['modify_price'];
            }
            if($original_price==0){
                $original_price='免费';
            }else{
                $original_price=GeneralTool::priceFormat($original_price).'';
            }
            $source_pic->original_price=$original_price;
            $source_pic->preferential_price=$original_price;
            if(!empty($source_pic->original_image)){
                $original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
                $source_pic->original_image=empty($original_image)?'':$original_image;
            }
            if(!empty($source_pic->thumbnail)){
                $thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
                $source_pic->thumbnail=empty($thumbnail)?'':$thumbnail;
            }
            if(!empty($source_pic->small_image)){
                $small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                $source_pic->small_image=empty($small_image)?'':$small_image;
            }
        }
        $source_pic_arr=$source_pics->toArray();
        if(empty($source_pic_arr['data'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0);
        }
        return array('data'=>$source_pic_arr['data'],'current_page'=>$source_pic_arr['current_page'],'per_page'=>$source_pic_arr['per_page'],'total_page'=>$source_pic_arr['last_page'],'total'=>$source_pic_arr['total']);
    }
    public function getMySourcePicOffTheShelvesList(array $data)
    {
        if(!isset($data['per_page'])||!isset($data['user_id'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>0,'total_page'=>0,'total'=>0);
        }
        $per_page=$data['per_page'];
        $user_id=$data['user_id'];
        //只获取未通过和审核中的首次啊
        $source_pics = DB::table('source_pics')->where(array('state' => 1,'audit_state' => 2,'is_shelves' => 0,'user_id' => $user_id))
            ->where('audit_state',2)
            ->select("source_pic_id","used_num as sell_num",'is_infinite_time','end_time','name as source_pic_name','is_shelves','audit_state',"original_image",'price as original_price','thumbnail','small_image','user_id')
            ->paginate($per_page);
        foreach ($source_pics as $source_pic){
            $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($source_pic->source_pic_id,0,1);
            $original_price=$source_pic->original_price;
            if($is_resource_modify_price!==false){
                $original_price=$is_resource_modify_price['modify_price'];
            }
            if($original_price==0){
                $original_price='免费';
            }else{
                $original_price=GeneralTool::priceFormat($original_price).'';
            }
            $source_pic->original_price=$original_price;
            $source_pic->preferential_price=$original_price;
            if(!empty($source_pic->original_image)){
                $original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
                $source_pic->original_image=empty($original_image)?'':$original_image;
            }
            if(!empty($source_pic->thumbnail)){
                $thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
                $source_pic->thumbnail=empty($thumbnail)?'':$thumbnail;
            }
            if(!empty($source_pic->small_image)){
                $small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                $source_pic->small_image=empty($small_image)?'':$small_image;
            }
        }
        $source_pic_arr=$source_pics->toArray();
        if(empty($source_pic_arr['data'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0);
        }
        return array('data'=>$source_pic_arr['data'],'current_page'=>$source_pic_arr['current_page'],'per_page'=>$source_pic_arr['per_page'],'total_page'=>$source_pic_arr['last_page'],'total'=>$source_pic_arr['total']);
    }
    /***
     * 获取我的素材列表
     * @param array $data
     * @return array
     */
    public function getMyAllSourcePicList(array $data)
    {
        if(!isset($data['per_page'])||!isset($data['user_id'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>0,'total_page'=>0,'total'=>0);
        }
        $per_page=$data['per_page'];
        $user_id=$data['user_id'];
        //只获取未通过和审核中的首次啊
        $source_pics = DB::table('source_pics')->where(array('state' => 1,'user_id' => $user_id,'audit_state' => 2))
            ->select("source_pic_id","used_num as sell_num",'is_infinite_time','end_time','name as source_pic_name','price as original_price',"original_image","is_shelves",'thumbnail','audit_state','small_image','user_id')
            ->paginate($per_page);
        foreach ($source_pics as $source_pic){
            $is_resource_modify_price=ModifyResourcePriceRecordsService::is_resource_modify_price($source_pic->source_pic_id,0,1);
            $original_price=$source_pic->original_price;
            if($is_resource_modify_price!==false){
                $original_price=$is_resource_modify_price['modify_price'];
            }
            if($original_price==0){
                $original_price='免费';
            }else{
                $original_price=GeneralTool::priceFormat($original_price).'';
            }
            $source_pic->original_price=$original_price;
            $source_pic->preferential_price=$original_price;
            if(!empty($source_pic->original_image)){
                $original_image=ImgTool::getImgUrlByImgPath($source_pic->original_image);
                $source_pic->original_image=empty($original_image)?'':$original_image;
            }
            if(!empty($source_pic->thumbnail)){
                $thumbnail=ImgTool::getImgUrlByImgPath($source_pic->thumbnail);
                $source_pic->thumbnail=empty($thumbnail)?'':$thumbnail;
            }
            if(!empty($source_pic->small_image)){
                $small_image=ImgTool::getImgUrlByImgPath($source_pic->small_image);
                $source_pic->small_image=empty($small_image)?'':$small_image;
            }
            $source_pic->audit=SourcePicTool::getAuditByAuditState($source_pic->audit_state);
            $source_pic->is_shelves_value=SourcePicTool::getIsShelvesState($source_pic->is_shelves,$source_pic->audit_state);
        }
        $source_pic_arr=$source_pics->toArray();
        if(empty($source_pic_arr['data'])){
            return array('data'=>array(),'current_page'=>1,'per_page'=>$per_page,'total_page'=>0,'total'=>0);
        }
        return array('data'=>$source_pic_arr['data'],'current_page'=>$source_pic_arr['current_page'],'per_page'=>$source_pic_arr['per_page'],'total_page'=>$source_pic_arr['last_page'],'total'=>$source_pic_arr['total']);
    }

    /***
     * id获取素材信息
     * @param $source_pic_id
     * @param int $state
     * @return null
     */
    public function  getSourcePicBySourcePicId($source_pic_id,$state=1){
        if(empty($source_pic_id)){
            return null;
        }
        $query=DB::table('source_pics')->where(array('source_pic_id'=>$source_pic_id));
        if($state===1){
            $query->where('state',1);
        }
        return $query->first();
    }
}
